/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: BaseCoreActivity.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.mvp;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;


import java.util.List;

import io.bhex.baselib.utils.DebugLog;

/**
 * 最基础的Activity类，所有原生Activity，都应继承他
 * ================================================
*/
public abstract class BaseCoreActivity extends AppCompatActivity {

    protected boolean isAlive = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DebugLog.i(getClass().getSimpleName() + "---onCreate---enter------");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        DebugLog.i(getClass().getSimpleName() + "---onRestart---enter------");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        DebugLog.i(getClass().getSimpleName() + "---onRestoreInstanceState---enter------");
    }

    @Override
    protected void onStart() {
        super.onStart();
        DebugLog.i(getClass().getSimpleName() + "---onStart---enter------");
    }

    @Override
    protected void onResume() {
        super.onResume();
        DebugLog.i(getClass().getSimpleName() + "---onResume---enter------");
    }

    @Override
    protected void onPause() {
        super.onPause();
        DebugLog.i(getClass().getSimpleName() + "----onPause--enter------");
    }

    @Override
    protected void onStop() {
        super.onStop();
        DebugLog.i(getClass().getSimpleName() + "---onStop---enter------");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        DebugLog.i(getClass().getSimpleName() + "---onConfigurationChanged---enter------");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        DebugLog.i(getClass().getSimpleName() + "---onSaveInstanceState---enter------");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DebugLog.i(getClass().getSimpleName() + "---onSaveInstanceState---enter------");

        isAlive = false;
    }

    /**
     * Activity 是否还可用
     *
     * @return
     */
    public boolean isAlive() {
        return isAlive && !isFinishing();
    }

    public Fragment instanceFragment(String fName, Bundle bundle, String tag) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment != null) {
            if (fragment.getArguments() != null)
                fragment.getArguments().putAll(bundle);
            return fragment;
        }

        List<Fragment> frgs = getSupportFragmentManager().getFragments();
        if (frgs != null) {
            for (Fragment item : frgs) {
                //这里有可能会有问题，因为多个相同的fragment到这里就会出错
                if (item ==null || !item.getClass().getName().equals(fName))
                    continue;

                if (item.getArguments() != null)
                    item.getArguments().putAll(bundle);
                return item;
            }
        }

        fragment = Fragment.instantiate(this, fName, bundle);

        return fragment;
    }

    public Fragment instanceFragment(String fName, Bundle bundle) {
        return instanceFragment(fName, bundle, fName);
    }
}
